<template>
  <div>
    <test t1="订单管理" t2="订单列表" path="/home">
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入搜索内容" v-model="queryInfo.query" @change="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表 -->
      <el-table :data="orderList" border style="width: 100%">
        <el-table-column type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === '1'" type="success">已付款</el-tag>
            <el-tag v-else type="danger">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间">
          <template slot-scope="scope">{{scope.row.create_time | dateFormat }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editDialogVisible = true"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-location-outline"
              size="mini"
              @click="showProgressBox"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <!-- layout 组件布局，子组件名用逗号分隔,表示要在页面渲染中使用的组件 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
      <!-- 修改地址对话框 -->
      <el-dialog title="修改地址" :visible.sync="editDialogVisible" width="50%" @close="closeReset">
        <el-form :model="editForm" :rules="editRules" ref="editRuleFormRef" label-width="100px">
          <el-form-item label="省市区/县" prop="address1">
            <el-cascader :options="cityData" v-model="editForm.address1"></el-cascader>
          </el-form-item>

          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="editForm.address2"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 展示物流进度对话框 -->
      <el-dialog title="物流进度" :visible.sync="progressDialogVisible" width="50%">
        <el-timeline :reverse="false">
          <el-timeline-item
            v-for="(activity, index) in progressList"
            :key="index"
            :timestamp="activity.time"
          >{{activity.context}}</el-timeline-item>
        </el-timeline>
        <span slot="footer" class="dialog-footer">
          <el-button @click="progressDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="progressDialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
    </test>
  </div>
</template>

<script>
import cityData from "./citydata";
export default {
  data() {
    return {
      // 获取订单列表请求数据
      queryInfo: {
        query: "",
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 5
      },
      // 订单列表数据
      orderList: [],
      // 订单总条数
      total: 0,
      // 省市联动数据
      cityData,
      editForm: {
        address1: [],
        address2: ""
      },
      editRules: {
        address1: [
          { required: true, message: "请选择省市区县", trigger: "blur" }
        ],
        address2: [
          { required: true, message: "请填写详细地址", trigger: "blur" }
        ]
      },
      editDialogVisible: false,
      progressDialogVisible: false,
      // 物流数据信息
      progressList: []
    };
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$axios.get("orders", {
        params: this.queryInfo
      });
    // console.log(res);
      if (res.meta.status !== 200)
        return this.$message.error("获取订单列表失败！");
      this.orderList = res.data.goods;
      this.total = res.data.total;
    },
    // 每页显示条数变化时触发
    handleSizeChange(val) {
      //   console.log(`每页 ${val} 条`);
      this.queryInfo.pagesize = val;
      this.getOrderList();
    },
    // 当前页码发生变化时触发
    handleCurrentChange(val) {
      //   console.log(`当前页: ${val}`);
      this.queryInfo.pagenum = val;
      this.getOrderList();
    },
    // 关闭对话框重置表单
    closeReset() {
      this.$refs.editRuleFormRef.resetFields();
    },
    async showProgressBox() {
      const { data: res } = await this.$axios.get("/kuaidi/1106975712662");
      // console.log(res);
      if (res.meta.status != 200) this.$message.error(res.meta.msg);
      this.progressList = res.data;
      this.progressDialogVisible = true;
    }
  },
  created() {
    this.getOrderList();
  }
};
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>